<?
include("./_common.php");
include("$g4[path]/_head.php");

$shop = get_shop();
$goods_dir = $g4[path]."/data/goods/";

$mid = $shop[shop_mid];			//Dacom 제공 상점아이디
$mertkey = $shop[shop_mertkey];		//데이콤에서 발급받은 키값

$shop_mode = $shop[shop_mode];

$home_url = "http://".$_SERVER["SERVER_NAME"]."/shop/order_success.php";
$note_url = "http://".$_SERVER["SERVER_NAME"]."/shop/note_url.php";		//결과 화면처리 페이지 (http 또는 https 로 시작하는 전체 URL 지정 : 직접제작)
$fail_url = "http://".$_SERVER["SERVER_NAME"]."/shop/order_fail.php"; //결제실패 처리 화면




if(trim($_SESSION["g4_cart"])!="")
{

	$oid = "O".time().rand(10,99);		//주문번호

	$CART = explode("/",$_SESSION["g4_cart"]);

	$tmpCart = explode("|",$CART[0]);

	$tmpCartGcode = explode("OP",$tmpCart[0]);

	$tmpGoods = get_goods($tmpCartGcode[0]);
	if(count($CART) > 1)
	{
		$o_subject_str = " 외 ".number_format(count($CART) - 1)."건";
	}

	$o_subject = $tmpGoods[g_name].$o_subject_str;
	$pay_price = str_replace(",","",$pay_price);
	$pay_tranc = str_replace(",","",$pay_tranc);
	$pay_point = str_replace(",","",$pay_point);
	$pay_amount = str_replace(",","",$pay_amount);
	$pay_pid = $pay_pid1.$pay_pid2;

	if($pay_method == "iche"){$pay_user = $pay_iche_user;}else{$pay_iche_user = "";}
	if($pay_method == "bank"){$pay_user = $bank_user;$pay_info = $bank_info;}else{$bank_user = ""; $bank_info = "";}
	$status = "0";
	$status_history = "0|".date("Y-m-d H:i:s");


	$sql = "insert into g4_order set ";
	$sql.= "oid = '".$oid."', ";
	$sql.= "mb_id = '".$mb_id."', ";
	$sql.= "mb_part = '".$mb_part."', ";
	$sql.= "o_subject = '".$o_subject."', ";
	$sql.= "o_name = '".$o_name."', ";
	$sql.= "o_email = '".$o_email."', ";
	$sql.= "o_tel = '".$o_tel."', ";
	$sql.= "o_hp = '".$o_hp."', ";
	$sql.= "o_zip = '".$o_zip."', ";
	$sql.= "o_addr1 = '".$o_addr1."', ";
	$sql.= "o_addr2 = '".$o_addr2."', ";
	$sql.= "s_name = '".$s_name."', ";
	$sql.= "s_email = '".$s_email."', ";
	$sql.= "s_tel = '".$s_tel."', ";
	$sql.= "s_hp = '".$s_hp."', ";
	$sql.= "s_zip = '".$s_zip."', ";
	$sql.= "s_addr1 = '".$s_addr1."', ";
	$sql.= "s_addr2 = '".$s_addr2."', ";
	$sql.= "s_memo = '".$s_memo."', ";
	$sql.= "pay_amount = '".$pay_amount."', ";
	$sql.= "pay_method = '".$pay_method."', ";
	$sql.= "pay_point = '".$pay_point."', ";
	$sql.= "pay_tranc = '".$pay_tranc."', ";
	$sql.= "pay_price = '".$pay_price."', ";
	$sql.= "pay_info = '".$pay_info."', ";
	$sql.= "status = '".$status."', ";
	$sql.= "status_history = '".$status_history."', ";
	$sql.= "pay_user = '".$pay_user."', ";
	$sql.= "pay_pid = '".$pay_pid."', ";
	$sql.= "remote_ip = '".$_SERVER["REMOTE_ADDR"]."', ";
	$sql.= "buy_datetime = now() ";

	$rst = mysql_query($sql);

	if($rst)
	{
		if(trim($_SESSION["g4_cart"]))
		{
			for($i=0;$i<count($CART);$i++)
			{
				$tmpRecord = explode("|",$CART[$i]);
				$tmpCartRow = explode("OP",$tmpRecord[0]);

				$tmpCartGoods = get_goods($tmpCartRow[0]);
				$tmpOption = get_option($tmpCartGoods[g_option],$tmpCartRow[1]);

				$tot_price = ($tmpCartGoods[g_price]+$tmpOption[1]) * $tmpRecord[1];
				$sql2 = "insert into g4_order_goods set ";
				$sql2.= "oid = '".$oid."', ";
				$sql2.= "mb_id = '".$mb_id."', ";
				$sql2.= "mb_part = '".$mb_part."', ";
				$sql2.= "g_code = '".$tmpCartGoods[g_code]."', ";
				$sql2.= "g_name = '".$tmpCartGoods[g_name]."', ";
				$sql2.= "g_option = '".$tmpOption[0]."|".$tmpOption[1]."', ";
				$sql2.= "cnt = '".$tmpRecord[1]."', ";
				$sql2.= "g_price = '".($tmpCartGoods[g_price]+$tmpOption[1])."', ";
				$sql2.= "g_tranc = '".$tmpCartGoods[g_tranc]."', ";
				$sql2.= "tot_price = '".$tot_price."', ";
				$sql2.= "buy_datetime = now() ";
				
				$rst2 = mysql_query($sql2);

				if($rst2)
				{
					$goods_sql = "update g4_goods set g_cnt = g_cnt - ".$tmpRecord[1].", g_sale = g_sale + ".$tmpRecord[1]." where g_code = '".$tmpCartGoods[g_code]."' limit 1";
					$goods_rst = mysql_query($goods_sql);
				}
			}
		}
	}

	if($mb_part == "member" && $pay_point > 0)
	{
		$sql3 = "insert into g4_point set ";
		$sql3.= "mb_id = '".$mb_id."', ";
		$sql3.= "po_datetime = now(), ";
		$sql3.= "po_content = '주문번호 [".$oid."]건 결제', ";
		$sql3.= "po_point = '-".$pay_point."', ";
		$sql3.= "po_rel_table = '@shop', ";
		$sql3.= "po_rel_id = 'system', ";
		$sql3.= "po_rel_action = '".date("Y-m-d")."' ";

		$rst3 = mysql_query($sql3);

		if($rst3)
		{
			$sql4 = "update g4_member set mb_point = mb_point - ".$pay_point." where mb_id = '".$mb_id."' limit 1";
			$rst4 = mysql_query($sql4);
		}
	}

	$_SESSION["g4_cart"] = "";
	$_SESSION["g4_oid"] = $oid;
	}
	elseif(trim($_SESSION["g4_oid"])!="")
	{
		$oid = trim($_SESSION["g4_oid"]);
	}
	else
	{
		echo("<script>alert('잘못된 접속입니다.');location.href='".$g4[path]."/shop/goods_list.php';</script>");
	}

if($oid != "")
{

	$sql = "select * from g4_order where oid='".$oid."' limit 1";
	$rst = mysql_query($sql);
	$order_row = mysql_fetch_array($rst);

	$hashdata = md5($mid.$oid.$order_row[pay_amount].$mertkey);
?>
<link rel="stylesheet" href="<?=$g4['path']?>/shop/style.css" type="text/css">
<script language="javascript" src="<?=$g4['path']?>/shop/shop.js"></script>
<DIV id="PG_BG_LAYER" style="z-index:99;width:1280;height:1024;position:absolute;left:0px;top:0px;background:#000000;display:none;filter:alpha(style='0', opacity='70');"></DIV>
<DIV id="PG_LAYER" style="z-index:100;position:absolute;width:330px;height:450px;left:50%;margin-left:-165px;top:100px;display:none;">
<DIV style="width:330px;height:20px;background:#3399FF;text-align:right"><span style="color:#FFFFFF;cursor:pointer" onclick="PG_CLOSE();">닫기</span></div>
<iframe name="payFrame" width="330" height="430" frameborder="0" scrolling="no"></iframe>
</DIV>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
	<tr>
		<td>
		<table cellpadding="0" cellspacing="1" border="0" width="100%" style="border:solid 1px #CCCCCC">
			<tr align="center" height="20">
				<td bgcolor="#EFEFEF">상품명</td>
				<td bgcolor="#EFEFEF" width="100">옵션</td>
				<td bgcolor="#EFEFEF" width="80">수량</td>
				<td bgcolor="#EFEFEF" width="80">단가</td>
				<td bgcolor="#EFEFEF" width="80">합계금액</td>
			</tr>
		</table>
		<?
			$order_goods_sql = "select * from g4_order_goods where oid = '".$oid."'";
			$order_goods_rst = mysql_query($order_goods_sql);

			$tot_price = 0;
			$tot_tranc = $shop[tranc_price];
			$lineCnt = 0;
			while($order_goods_row = mysql_fetch_array($order_goods_rst))
			{
				$g_option = explode("|",$order_goods_row[g_option]);
		?>
		<DIV style="width:100%;height:50px;float:left;padding:5 0 5 0;border-bottom:solid 1px #D7D7D7">
		<table cellpadding="0" cellspacing="0" border="0" width="100%" height="50" style="table-layout:fixed">
			<tr>
				<td>
					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<td><b><?=$order_goods_row[g_name]?></b></td>
							<td width="100" align="center"><?if($g_option[0]!=""){?><?=$g_option[0]?>(+<?=$g_option[1]?>)<?}?></td>
							<td width="80" align="center"><?=$order_goods_row[cnt]?>EA</td>
							<td width="80" align="center"><span style="color:#990000"><?=number_format($order_goods_row[g_price])?>원</span></td>
							<td width="80" align="center"><span style="color:#990000"><b><?=number_format($order_goods_row[g_price] * $order_goods_row[cnt])?>원</b></span></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
		</DIV>
		<?
				$tot_price+= $order_goods_row[g_price] * $order_goods_row[cnt];
				if($tot_price >= $shop[tranc_if] || $order_goods_row[g_tranc] == "N" || $order_goods_row[g_tranc] == "C")
				{
					$tot_tranc = 0;
				}
				$lineCnt++;
			}

		if($lineCnt==0)
		{
			echo("<table cellpadding='0' cellspacing='0' border='0' width='100%' height='100'><tr align='center'><td>장바구니에 상품이 없습니다.</td></tr></table>");
		}
		?>
		</td>
	</tr>
	<tr>
		<td>
			<table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#EFEFEF">
				<tr align="right" height="30">
					<td>배송비 : <?=number_format($tot_tranc)?>원</td>
					<td>총구매금액 : <?=number_format($tot_price)?>원</td>
				</tr>
				<tr align="right" height="30">
					<td colspan="2"><span style="color:#990000;font-size:16px;font-weight:bold">총금액 : <?=number_format($tot_tranc + $tot_price)?>원</span></td>
				</tr>
			</table>
		<tr>
	</tr>
	<tr>
		<td height="20"></td>
	</tr>
	<tr>
		<td>
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<td><span class="orderTabTitle">결제정보</span></td>
				</tr>
				<tr>
					<td>
						<table cellpadding="5" cellspacing="0" border="0" width="100%" style="border:solid 1px #D7D7D7">
							<tr>
								<td class="orderIndex">총주문금액</td>
								<td><?=number_format($order_row[pay_price])?>원</td>
							</tr>
							<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
							<tr>
								<td class="orderIndex">배송비</td>
								<td><?=number_format($order_row[pay_tranc])?>원</td>
							</tr>
							<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
							<tr>
								<td class="orderIndex">결제포인트</td>
								<td><?=number_format($order_row[pay_point])?>원</td>
							</tr>
							<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
							<tr>
								<td class="orderIndex">결제금액</td>
								<td><?=number_format($order_row[pay_amount])?><span style="font-size:14px;font-weight:bold;color:#990000">원</span></td>
							</tr>
							<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
							<tr>
								<td class="orderIndex">결제방법</td>
								<td>
								<?=get_paymethod($order_row[pay_method])?>&nbsp;&nbsp;&nbsp;
								<?if($order_row[pay_method] == "bank"){?>
								입금계좌 : <?=str_replace("|","&nbsp;&nbsp;",$order_row[pay_info])?>&nbsp;&nbsp;&nbsp;입금자명 : <?=$order_row[pay_user]?>
								<?}elseif($order_row[pay_method] == "iche"){?>
								결제자명 : <?=$order_row[pay_user]?>&nbsp;&nbsp;&nbsp;주민번호 : <?=$order_row[pay_pid]?>
								<?}else{?>
								결제자명 : <?=$order_row[pay_user]?>
								<?}?>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td align="center" height="50">
		<button class="cart_btn1" onclick="order_step4();">결제하기</button>&nbsp;
		<button class="cart_btn1" onclick="if(confirm('취소 하시겠습니까?')){location.href='order_cancel.php';}">취소</button>
		</td>
	</tr>
</table>

<!-- 데이콤 결제 폼 -->
<form name="payForm" method="post" target="payFrame">
<input type="hidden" name="shop_mode" value="<?= $shop_mode ?>">
<input type="hidden" name="pay_method" value="<?= $order_row[pay_method] ?>">
<input type="hidden" name="hashdata" value="<?= $hashdata ?>">		<!-- 결제요청 검증(무결성) 필드-->
<input type="hidden" name="mid" value="<?= $mid?>">						<!-- 상점ID -->
<input type="hidden" name="oid" value="<?= $oid?>">						<!-- 주문번호 -->
<input type="hidden" name="amount" value="<?=$order_row[pay_amount]?>">								<!-- 결제금액 -->
<input type="hidden" name="buyer" value="<?=$order_row[pay_user]?>">									<!-- 구매자 -->
<input type="hidden" name="pid" value="<?=$order_row[pay_pid]?>">										<!-- 구매자 주민번호-->
<input type="hidden" name="productinfo" value="<?=$order_row[o_subject]?>">							<!-- 상품명 -->

<input type="hidden" name="home_url" value="<?= $home_url?>">		<!-- 팝업창 사용시 화면처리 URL (프레임 이용시 home_url 사용) -->
<input type="hidden" name="note_url" value="<?= $note_url?>">			<!-- 결제결과 DB처리 URL(웹전송연동방식일때 : 필수) -->
<input type="hidden" name="fail_url" value="<?= $fail_url?>">			<!-- 실패처리 URL(웹전송연동방식일때 : 필수) -->

<input type="hidden" name="buyeremail" value="<?=$order_row[o_email]?>">

</form>
<?
}
else
{
	echo("<script>alert('잘못된 접속입니다.');location.href='".$g4[path]."/shop/goods_list.php';</script>");
}
include("$g4[path]/_tail.php");

?>